package com.xmu.prcs.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;

import com.xmu.prcs.action.bean.FeatureBean;
import com.xmu.prcs.action.bean.RetrievalResultBean;
import com.xmu.prcs.dao.BodyDao;
import com.xmu.prcs.dao.FeatureDao;
import com.xmu.prcs.dao.PartDao;
import com.xmu.prcs.dao.PlantDao;
import com.xmu.prcs.dao.PlantFeatureDao;
import com.xmu.prcs.entity.Body;
import com.xmu.prcs.entity.Feature;
import com.xmu.prcs.entity.Part;
import com.xmu.prcs.entity.Plant;
import com.xmu.prcs.entity.PlantFeature;
import com.xmu.prcs.service.RetrievalManager;

public class RetrievalManagerImpl implements RetrievalManager {

	private PartDao partDao;
	private BodyDao bodyDao;
	private FeatureDao featureDao;
	private PlantFeatureDao plantFeatureDao;
	private PlantDao plantDao;
	
	public PartDao getPartDao() {
		return partDao;
	}

	public void setPartDao(PartDao partDao) {
		this.partDao = partDao;
	}

	public BodyDao getBodyDao() {
		return bodyDao;
	}

	public void setBodyDao(BodyDao bodyDao) {
		this.bodyDao = bodyDao;
	}

	public FeatureDao getFeatureDao() {
		return featureDao;
	}

	public void setFeatureDao(FeatureDao featureDao) {
		this.featureDao = featureDao;
	}

	public PlantFeatureDao getPlantFeatureDao() {
		return plantFeatureDao;
	}

	public void setPlantFeatureDao(PlantFeatureDao plantFeatureDao) {
		this.plantFeatureDao = plantFeatureDao;
	}

	public PlantDao getPlantDao() {
		return plantDao;
	}

	public void setPlantDao(PlantDao plantDao) {
		this.plantDao = plantDao;
	}

	public List<FeatureBean> getMultiFeature() {
		List<FeatureBean> beans = null;
		beans = this.getTwoLevel(); //先得到二级性状
		beans.addAll(this.getMoreLevel());//再加多级性状
		
		return beans;
	}

	/**
	 * 得到二级性状
	 * @return
	 */
	public List<FeatureBean> getTwoLevel(){
		List<FeatureBean> beans = new ArrayList<FeatureBean>();
		List<Part> twoLevel = partDao.getPartOfLevelTwo();
		FeatureBean bean;
		if(twoLevel != null && twoLevel.size() != 0){
			for(Part part : twoLevel){
				bean = new FeatureBean();
				bean.setGrade(50);
				bean.setIdLevel(part.getIdPart());
				bean.setLevelName(part.getPartName());
				bean.setIsLastLevel(0);
				bean.setSecond(true);
				
				Set<Feature> fsets = part.getFeatures();
				if(fsets != null && fsets.size() != 0){
					List<FeatureBean> second = new ArrayList<FeatureBean>();
					FeatureBean feature;
					for(Feature f : fsets){
						feature = new FeatureBean();
						feature.setGrade(f.getDefaultGrade());
						feature.setIdLevel(f.getIdFeature());
						feature.setLevelName(f.getFeatureInfo());
						Set<Feature> children = f.getChild();
						if(children == null || children.size() == 0){
							feature.setIsLastLevel(1);
						}else{
							feature.setIsLastLevel(0);
							List<FeatureBean> flist3 = new ArrayList<FeatureBean>();
							FeatureBean f3;
							for(Feature child : children){
								f3 = new FeatureBean();
								f3.setGrade(child.getDefaultGrade());
								f3.setIdLevel(child.getIdFeature());
								f3.setIsLastLevel(1);
								f3.setLevelName(child.getFeatureInfo());
								flist3.add(f3);
							}
							feature.setChildren(flist3);
						}
						second.add(feature);
					}
					bean.setChildren(second);
				}
				beans.add(bean);
			}
		}
		return beans;
	}
	
	/**
	 * 得到三级及以上的性状，最多五级
	 * @return
	 */
	public List<FeatureBean> getMoreLevel(){
		List<FeatureBean> beans = new ArrayList<FeatureBean>();
		FeatureBean f1;
		List<Body> bodys = bodyDao.getAll();
		if(bodys != null && bodys.size() != 0){
			for(Body body : bodys){//第一级
				f1 = new FeatureBean();
				f1.setGrade(50);
				f1.setIdLevel(body.getIdBody());
				f1.setLevelName(body.getBodyName());
				Set<Part> parts = body.getParts();
				if(parts != null && parts.size() !=0){
					f1.setIsLastLevel(0);
					List<FeatureBean> flist2 = new ArrayList<FeatureBean>();
					FeatureBean f2;
					for(Part part : parts){//第二级
						f2 = new FeatureBean();
						f2.setGrade(50);
						f2.setIdLevel(part.getIdPart());
						f2.setLevelName(part.getPartName());
						f2.setSecond(true);
						Set<Feature> features = part.getFeatures();
						if(features != null && features.size() != 0){
							f2.setIsLastLevel(0);
							List<FeatureBean> flist3 = new ArrayList<FeatureBean>();
							FeatureBean f3;
							for(Feature feature : features){//第三级
								f3 = new FeatureBean();
								f3.setGrade(feature.getDefaultGrade());
								f3.setIdLevel(feature.getIdFeature());
								f3.setLevelName(feature.getFeatureInfo());
								Set<Feature> fchild = feature.getChild();
								if(fchild != null && fchild.size() != 0){
									f3.setIsLastLevel(0);
									List<FeatureBean> flist4 = new ArrayList<FeatureBean>();
									FeatureBean f4;
									for(Feature child1 : fchild){//第四级
										f4 = new FeatureBean();
										f4.setGrade(child1.getDefaultGrade());
										f4.setIdLevel(child1.getIdFeature());
										f4.setLevelName(child1.getFeatureInfo());
										Set<Feature> fchild2 = child1.getChild();
										if(fchild2 != null && fchild2.size() != 0){
											f4.setIsLastLevel(0);
											List<FeatureBean> flist5 = new ArrayList<FeatureBean>();
											FeatureBean f5;
											for(Feature child2 : fchild2){//第五级，最多五级
												f5 = new FeatureBean();
												f5.setGrade(child2.getDefaultGrade());
												f5.setIdLevel(child2.getIdFeature());
												f5.setLevelName(child2.getFeatureInfo());
												f5.setIsLastLevel(1);
												flist5.add(f5);
											}
											f4.setChildren(flist5);
										}else{
											f4.setIsLastLevel(1);
										}
										flist4.add(f4);
									}
									f3.setChildren(flist4);
								}else{
									f3.setIsLastLevel(1);
								}
								flist3.add(f3);
							}
							f2.setChildren(flist3);
						}else{
							f2.setIsLastLevel(1);
						}
						flist2.add(f2);
					}
					f1.setChildren(flist2);
				}else{
					f1.setIsLastLevel(1);
				}
				beans.add(f1);
			}
		}
		return beans;
	}
	
	public void addFeature4Plant(int plantId, List<FeatureBean> features) {
		if(plantId > 0 && features != null){
			Plant plant = plantDao.get(plantId);
			for(FeatureBean featureBean : features){
				Feature feature = featureDao.get(featureBean.getIdLevel());
				PlantFeature plantFeature = new PlantFeature();
				plantFeature.setFeature(feature);
				plantFeature.setGrade(featureBean.getGrade());
				plantFeature.setPlant(plant);
				plantFeatureDao.save(plantFeature);
			}
		}
	}

	public List<RetrievalResultBean> listMultiChannelRetrieval(
			List<String> featureId, List<String> number,
			List<String> numberFeature) {
		/** 检索结果中，拥有重要特性的植物置顶并按性状重要性总得分排序，其他植物按性状重要性总得分排序  **/
		List<PlantFeature> features = plantFeatureDao.getByFeatureIds(featureId);//得到全部检索结果
		
		if(features == null || features.size() == 0){
			return null;
		}
		
		List<PlantFeature> important = plantFeatureDao.getMostImportantFeature(featureId);//拥有特殊性状的检索结果
		//去掉重复的性状，
		if(important != null && important.size() != 0){
			for(PlantFeature im : important){
				for(int i = 0 ; i < features.size(); i++){
					if(im.getPlant().getIdPlant().equals(features.get(i).getPlant().getIdPlant())){
						im.setGrade(features.get(i).getGrade());
						features.remove(i);
						break;
					}
				}
			}
		}
		//考虑数值型性状，features中的检索结果并没有对数值型性状的数值进行过滤，
		//应将不再数值型性状数值范围内的检索结果过滤，减去该数值型性状重要性得分即可
		if(numberFeature != null && numberFeature.size() != 0){
			for(int i = 0; i < numberFeature.size(); i++){
				List<PlantFeature> pf = plantFeatureDao.getNotInNumberRange(Integer.parseInt(numberFeature.get(i)), number.get(i));
				if(pf != null && pf.size() != 0){
					for(PlantFeature plantF : pf){
						for(int j = 0 ; j < features.size(); j++){
							if(plantF.getPlant().getIdPlant().equals(features.get(i).getPlant().getIdPlant())){
								features.get(i).setGrade(features.get(i).getGrade() - plantF.getGrade());
							}
						}
					}
				}
			}
		}
		
		/* 转换成RetrievalResultBean，并按照性状重要性排序 */
		List<RetrievalResultBean> result = new ArrayList<RetrievalResultBean>();
		
		List<RetrievalResultBean> importantResult = this.PlantFeature2RetrievalResultBean(important);
		Collections.sort(importantResult);
		List<RetrievalResultBean> featureResult = this.PlantFeature2RetrievalResultBean(features);
		Collections.sort(featureResult);
		//拥有选中的特殊性状的检索结果置顶
		result.addAll(importantResult);
		result.addAll(featureResult);
		return result;
	}
	
	public List<RetrievalResultBean> PlantFeature2RetrievalResultBean(List<PlantFeature> features){
		List<RetrievalResultBean> result = new ArrayList<RetrievalResultBean>();
		
		if(features != null && features.size() != 0){
			RetrievalResultBean bean;
			for(PlantFeature feature : features){
				bean = new RetrievalResultBean();
				bean.setIdPlant(feature.getPlant().getIdPlant());
				bean.setPlantName(feature.getPlant().getPlantName());
				bean.setSumGrade(feature.getGrade());
				result.add(bean);
			}
		}
		
		return result;
	}

	public String getPlantFeatures4RetrievalOfXMLString(){
		List<FeatureBean> list = getMultiFeature();
		StringBuffer sb = new StringBuffer("<?xml version='1.0' encoding='iso-8859-1' ?><tree id='0'>");
		if(list != null && list.size() != 0){
			for(FeatureBean f1 : list){//第一级
				if(f1.isSecond()){
					sb.append("<item text='" + f1.getLevelName() + "' id='" + 80000 + f1.getIdLevel() + "'>");
				}else{
					sb.append("<item text='" + f1.getLevelName() + "' id='" + 40000 + f1.getIdLevel() + "'>");
				}
				List<FeatureBean> flist2 = f1.getChildren();
				if(flist2 != null && flist2.size() != 0){
					for(FeatureBean f2 : flist2){//第二级
						
						if(f2.getIsLastLevel() == 1){//第二级的根结点
							sb.append("<item text='" + f2.getLevelName() + "' id='" + f2.getIdLevel() + "'/>");
						}else{
							if(f2.isSecond()){
								sb.append("<item text='" + f2.getLevelName() + "' id='" + 80000 + f2.getIdLevel() + "'>");
							}else{
								sb.append("<item text='" + f2.getLevelName() + "' id='" + f2.getIdLevel() + "'>");
							}
							List<FeatureBean> flist3 = f2.getChildren();
							for(FeatureBean f3 : flist3){//第三级
								sb.append("<item text='" + f3.getLevelName() + "' id='" + f3.getIdLevel());
								if(f3.getIsLastLevel() == 1){//第三级的根结点
									sb.append("'/>");
								}else{
									sb.append("'>");
									List<FeatureBean> flist4 = f3.getChildren();
									for(FeatureBean f4 : flist4){//第四级
										sb.append("<item text='" + f4.getLevelName() + "' id='" + f4.getIdLevel());
										if(f4.getIsLastLevel() == 1){//第四级的根结点
											sb.append("'/>");
										}else{
											sb.append("'>");
											List<FeatureBean> flist5 = f4.getChildren();
											for(FeatureBean f5 : flist5){//最多五级
												sb.append("<item text='" + f5.getLevelName() + "' id='" + f5.getIdLevel() + "'/>");
											}
											sb.append("</item>");
										}
									}
									sb.append("</item>");
								}
							}
							sb.append("</item>");
						}
					}
				}
				sb.append("</item>");
			}
		}
		sb.append("</tree>");
		
		return sb.toString();
	}
}
